using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Seems.Framework.Extensions;
using Seems.SL.VM;
using Seems.SL.View;

namespace Seems.View.Behaviors
{
	public class ZoomOnWheelBehavior: Behavior<UIElement>
	{
		protected override void OnAttached()
		{
			AssociatedObject.MouseWheel += MouseWheel;
		}

		protected override void OnDetaching()
		{
			AssociatedObject.MouseWheel -= MouseWheel;
		}

		private void MouseWheel(object sender, MouseWheelEventArgs e)
		{
			var view = AssociatedObject.AncestorsAndSelf<DesignerView>().First();
			var diagramVM = (DiagramVM)view.DataContext;

			var delta = diagramVM.ScaleSmallChange;
			diagramVM.Scale = e.Delta > 0 ? Math.Min(diagramVM.ScaleMax, diagramVM.Scale + delta) : Math.Max(diagramVM.ScaleMin, diagramVM.Scale - delta);

			e.Handled = true;
		}
	}
}